﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;
    using System.Windows.Forms;

    public class clsMauHoaDon
    {
        private string mDienGiai = "";
        private string mKhoaLoaiHoaDonIn = "";
        private string mMauHoaDon = "";
        private string mTenReport = "";

        public bool CheckNgayThang(string Khoa, string pNgayThang, string pMauHoaDon, string pSoKiHieu, string pSoHoaDon)
        {
            bool flag=false;
            string commandText = "";
            string left = "";
            string right = "";
            string ngayChungTu = "";
            string str = "";
            IDataParameter parameter = null;
            clsXuatKho kho = new clsXuatKho();
            try
            {
                commandText = "SELECT Isnull(Max(NgayHoaDon),'') FROM ST_XuatKho WHERE Rtrim(MauHoaDon) = '" + pMauHoaDon.Trim() + "' AND Rtrim(SoSeri) = '" + pSoKiHieu.Trim() + "'";
                left = Conversions.ToString(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }));
                right = this.GetNgayTheoSoHD(pMauHoaDon, pSoKiHieu, pSoHoaDon);
                kho.Load(Khoa);
                ngayChungTu = kho.NgayChungTu;
                if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(left, right, false) > 0)
                {
                    str = left;
                }
                else
                {
                    str = right;
                }
                if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(str, ngayChungTu, false) < 0)
                {
                    str = ngayChungTu;
                }
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(LVYModule.ReturnIfNull(str, "", modGeneral.TypeOfValue.Nothing), "", false))
                {
                    return true;
                }
                if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(pNgayThang, str, false) < 0)
                {
                    LVYModule.ShowWarning("Ng\x00e0y h\x00f3a đơn phải từ ng\x00e0y " + LVYModule.Char2Date(str));
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                flag = true;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool CreateMauHoaDon(string pLoaiHoaDon, string pSoLienIn)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[8];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@LoaiHoaDon", DbType.Int64, ParameterDirection.Input, pLoaiHoaDon);
                commandParameters[1] = DBHelper.createParameter("@SoLienIn", DbType.Int16, ParameterDirection.Input, pSoLienIn);
                commandParameters[2] = DBHelper.createParameter("@KhoaDonVi", DbType.Int64, ParameterDirection.Input, modGeneral.H_CLIENT);
                commandParameters[3] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                bool flag2 = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "sp_HT_MauHoaDon", commandParameters) > 0;
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool DelData(string pKhoa)
        {
            IDataParameter parameter = null;
            bool flag=false;
            string commandText = "";
            try
            {
                commandText = "DELETE FROM HT_MauHoaDon WHERE MauHoaDon = '" + pKhoa.Trim() + "'";
                bool flag2 = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }) > 0;
                flag = flag2;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetData_ThongBaoPhatHanh(string strCondition = "")
        {
            DataTable table;
            string commandText = "";
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " AND " + strCondition;
                }
                commandText = " SELECT LHD.Ten as LoaiHoaDon, DK.MauHoaDon, DK.SoKiHieu, DK.SoLuong, right('0000000' + CAST(DK.TuSo as varchar(10)),7) as TuSo, right('0000000' + CAST(DK.DenSo as varchar(10)),7) as DenSo, dbo.Char2Date(DK.NgayBDSD) as NgayBDSD FROM HT_DangKyHoaDon DK LEFT JOIN HT_LoaiHoaDonIn LHD on LHD.Khoa = DK.KhoaLoaiHoaDonIn WHERE 1 = 1 " + str2 + " ORDER BY LHD.Ten, DK.MauHoaDon, DK.SoKIHieu, DK.NgayBDSD ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public string GetNgayTheoSoHD(string pMauHoaDon, string pSoKiHieu, string pSoHoaDon)
        {
            string str;
            string commandText = "";
            try
            {
                int num = Conversions.ToInteger(pSoHoaDon);
                commandText = "SELECT Isnull(NgayBDSD,'') FROM HT_DangKyHoaDon WHERE Rtrim(MauHoaDon) = '" + pMauHoaDon.Trim() + "' AND Rtrim(SoKiHieu) = '" + pSoKiHieu.Trim() + "' AND TuSo <= " + Conversions.ToString(num) + " AND DenSo >= " + Conversions.ToString(num);
                str = Conversions.ToString(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                str = "";
                ProjectData.ClearProjectError();
                return str;
                ProjectData.ClearProjectError();
            }
            return str;
        }

        public string GetTextBarCode(string str1, string str2, string str3)
        {
            string str = "";
            str = modGeneral.H_TAXCODE + "0303235868" + str1 + "dinh hack ha ku" + str3 + "may di chet di" + str2 + "060005972941";
            return LVYModule.sLeft(this.MD5(this.MD5(str)), 0x10);
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[2];
            try
            {
                string commandText = "SELECT * FROM HT_MauHoaDon WHERE MauHoaDon = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, commandParameters);
                if (dataReader.Read())
                {
                    this.mMauHoaDon = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mDienGiai = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mKhoaLoaiHoaDonIn = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mTenReport = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public string MD5(string str)
        {
            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            string str3 = "";
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            byte[] buffer = provider.ComputeHash(bytes);
            provider.Clear();
            int num2 = buffer.Length - 1;
            for (int i = 0; i <= num2; i++)
            {
                str3 = str3 + buffer[i].ToString("x").PadLeft(2, '0');
            }
            return str3;
        }

        public DataTable ShowList(string strCondition = "")
        {
            DataTable table;
            string commandText = "";
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " AND " + strCondition;
                }
                commandText = " SELECT Rtrim(MauHoaDon) as Khoa, Rtrim(MauHoaDon) as Ten, Rtrim(TenReport) as TenReport  FROM HT_MauHoaDon WHERE 1 = 1 " + str2 + " ORDER BY 1 ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable ShowListAll(string strCondition = "")
        {
            DataTable table;
            string commandText = "";
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " AND KhoaLoaiHoaDonIn = '" + strCondition + "'";
                }
                commandText = " SELECT * FROM HT_MauHoaDon WHERE 1 = 1 " + str2 + " ORDER BY 1 ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool TrungMa(string pMauHoaDon)
        {
            bool flag2;
            string commandText = "";
            IDataParameter parameter = null;
            try
            {
                commandText = "SELECT * FROM HT_MauHoaDon WHERE RTRIM(MauHoaDon) = '" + pMauHoaDon.Trim() + "'";
                bool flag = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0].Rows.Count > 0;
                flag2 = flag;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                flag2 = true;
                ProjectData.ClearProjectError();
                return flag2;
                ProjectData.ClearProjectError();
            }
            return flag2;
        }

        public bool WasUsed(string pKhoa)
        {
            bool flag2;
            string commandText = "";
            IDataParameter parameter = null;
            try
            {
                commandText = "SELECT * FROM HT_DangKyHoaDon WHERE RTRIM(MauHoaDon) = '" + pKhoa.Trim() + "'";
                bool flag = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0].Rows.Count > 0;
                flag2 = flag;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                flag2 = true;
                ProjectData.ClearProjectError();
                return flag2;
                ProjectData.ClearProjectError();
            }
            return flag2;
        }

        public string DienGiai
        {
            get
            {
                return this.mDienGiai;
            }
            set
            {
                this.mDienGiai = value;
            }
        }

        public string KhoaLoaiHoaDonIn
        {
            get
            {
                return this.mKhoaLoaiHoaDonIn;
            }
            set
            {
                this.mKhoaLoaiHoaDonIn = value;
            }
        }

        public string MauHoaDon
        {
            get
            {
                return this.mMauHoaDon;
            }
            set
            {
                this.mMauHoaDon = value;
            }
        }

        public string TenReport
        {
            get
            {
                return this.mTenReport;
            }
            set
            {
                this.mTenReport = value;
            }
        }
    }
}

